//程序加载初始化here
function urlParse(){
    var params = {};
    if(window.location == null){
        return params;
    }
    var name,value; 
    var str=window.location.href; //取得整个地址栏
    var num=str.indexOf("?") 
    str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]

    var arr=str.split("&"); //各个参数放到数组里
    for(var i=0;i < arr.length;i++){ 
        num=arr[i].indexOf("="); 
        if(num>0){ 
            name=arr[i].substring(0,num);
            value=arr[i].substr(num+1);
            params[name]=value;
        } 
    }
    return params;
}
function initMgr(){
    cc.vv = {};
    // var UserMgr = require("UserMgr");
    // cc.vv.userMgr = new UserMgr();
    
    // var ReplayMgr = require("ReplayMgr");
    // cc.vv.replayMgr = new ReplayMgr();
    
    cc.vv.http = require("HTTP");
    cc.vv.global = require("Global");
    cc.vv.net = require("Net");
    
    //麻将网络管理
    var GameNetMgr = require("szmjNetManager");
    cc.vv.gameNetMgr = new GameNetMgr();
    cc.vv.gameNetMgr.initHandlers();
    
    // var AnysdkMgr = require("AnysdkMgr");
    // cc.vv.anysdkMgr = new AnysdkMgr();
    // cc.vv.anysdkMgr.init();
    
    // var VoiceMgr = require("VoiceMgr");
    // cc.vv.voiceMgr = new VoiceMgr();
    // cc.vv.voiceMgr.init();
    
    // var AudioMgr = require("AudioMgr");
    // cc.vv.audioMgr = new AudioMgr();
    // cc.vv.audioMgr.init();
    
    var Utils = require("Utils");
    cc.vv.utils = new Utils();
    
    //var MJUtil = require("MJUtil");
    //cc.vv.mjutil = new MJUtil();
    
    //cc.args = urlParse();
}

cc.Class({
    extends: cc.Component,

    properties: {
        nums:{
            default:[],
            type:[cc.Label]
        },
        _inputIndex:0,
        // foo: {
        //    default: null,
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        initMgr();
        var url = window.location.href;    //通过这个地址解析得到用户的openid
        
        var arr=url.split("/"); //各个参数放到数组里
        var arr_size = arr.length;
        if (arr_size >0) {
            cc.openId = arr[arr_size-1];
        }else
        {
            cc.openId = 0;
        }

        this.node.active = true;
    },
    
    onEnable:function(){
        this.onResetClicked();
    },
    
    onInputFinished:function(roomId){
        this.onEnter(roomId);
        // cc.vv.userMgr.getUserInfo(roomId,function(ret){
        //     if(ret != null){
        //         console.log("onInputFinished: " +ret);
        //         this.onEnterRoom(roomId);
        //     }
        //     else{
                
        //         this.onResetClicked();
        //     }
        // }.bind(this)); 
    },

    onEnter:function(roomId){
        console.log("onEnter");
        var data = {
            ip:'118.25.215.104',
            port:'3000',
            roomid:roomId,
        }
        cc.vv.gameNetMgr.connectGameServer(data);
    },

    onEnterRoom:function(roomId){
        cc.vv.userMgr.enterRoom(roomId,function(ret){
            console.log("onEnterRoom: " +ret);
            if(ret != null && this.node.active){
                this.node.active = false;
                //enter game
                console.log("onEnterRoomFinished: " +ret);
                cc.vv.gameNetMgr.connectGameServer(ret);
                cc.director.loadScene("mjgame");
            }
            else{
                // var content = "房间["+ roomId +"]不存在，请重新输入!";
                // if(ret.errcode == 4){
                //     content = "房间["+ roomId + "]已满!";
                // }
                //cc.vv.alert.show("提示",content);
                this.onResetClicked();
            }
        }.bind(this)); 
    },
    
    onInput:function(num){
        if(this._inputIndex >= this.nums.length){
            return;
        }
        this.nums[this._inputIndex].string = num;
        this._inputIndex += 1;
        
        if(this._inputIndex == this.nums.length){
            var roomId = this.parseRoomID();
            console.log("ok:" + roomId);
            this.onInputFinished(roomId);
        }
    },
    
    onN0Clicked:function(){
        this.onInput(0);  
    },
    onN1Clicked:function(){
        this.onInput(1);  
    },
    onN2Clicked:function(){
        this.onInput(2);
    },
    onN3Clicked:function(){
        this.onInput(3);
    },
    onN4Clicked:function(){
        this.onInput(4);
    },
    onN5Clicked:function(){
        this.onInput(5);
    },
    onN6Clicked:function(){
        this.onInput(6);
    },
    onN7Clicked:function(){
        this.onInput(7);
    },
    onN8Clicked:function(){
        this.onInput(8);
    },
    onN9Clicked:function(){
        this.onInput(9);
    },
    onResetClicked:function(){
        for(var i = 0; i < this.nums.length; ++i){
            this.nums[i].string = "";
        }
        this._inputIndex = 0;
    },
    onDelClicked:function(){
        if(this._inputIndex > 0){
            this._inputIndex -= 1;
            this.nums[this._inputIndex].string = "";
        }
    },
    onCloseClicked:function(){
        this.node.active = false;
    },
    
    parseRoomID:function(){
        var str = "";
        for(var i = 0; i < this.nums.length; ++i){
            str += this.nums[i].string;
        }
        return str;
    }

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
